package com.tianya.scheduled.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 统一格式返回
 *
 * ClassName: Result <br/>
 * Description: <br/>
 * date: 2021/5/21 22:03<br/>
 *
 * @author tianya<br />
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {

    private static int SUCCESS = 1;
    private static int FAIL = 0;

    private T data;

    private Integer code;

    private String msg;

    public static <T> Result<T> succeed() {
        return succeedWith(null, SUCCESS, "");
    }

    public static <T> Result<T> succeed(String msg) {
        return succeedWith(null, SUCCESS, msg);
    }

    public static <T> Result<T> succeed(T model, String msg) {
        return succeedWith(model, SUCCESS, msg);
    }

    public static <T> Result<T> succeedStr(T model) {
        return succeedWith(model, SUCCESS, "");
    }

    public static <T> Result<T> succeed(T model) {
        return succeedWith(model, SUCCESS, "");
    }

    public static <T> Result<T> succeedWith(T datas, Integer code, String msg) {
        return new Result<>(datas, code, msg);
    }

    public static <T> Result<T> failed(String msg) {
        return failedWith(null, FAIL, msg);
    }

    public static <T> Result<T> failed(T model, String msg) {
        return failedWith(model, FAIL, msg);
    }

    public static <T> Result<T> failedWith(T datas, Integer code, String msg) {
        return new Result<>(datas, code, msg);
    }

    public static boolean isSucceed(Result result) {
        return result != null && result.getCode() == Result.SUCCESS;
    }

}
